﻿//Thanks to Rob Conery
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using Press.MVC.Services;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.Generic;

public static class AppHelper
{
    /// <summary>
    /// Returns an absolute reference to the Content directory
    /// </summary>
    public static string ContentRoot
    {
        get
        {
            string contentVirtualRoot = "~/Content";
            return VirtualPathUtility.ToAbsolute(contentVirtualRoot);
        }
    }

    /// <summary>
    /// Returns an absolute reference to the Images directory
    /// </summary>
    public static string ImageRoot
    {
        get
        {
            return string.Format("{0}/{1}", ContentRoot, "Images");
        }
    }

    /// <summary>
    /// Returns an absolute reference to the CSS directory
    /// </summary>
    public static string CssRoot
    {
        get
        {
            return string.Format("{0}/{1}", ContentRoot, "CSS");
        }
    }

    /// <summary>
    /// Builds an Image URL
    /// </summary>
    /// <param name="imageFile">The file name of the image</param>
    public static string ImageUrl(string imageFile)
    {
        string result = string.Format("{0}/{1}", ImageRoot, imageFile);
        return result;
    }

    /// <summary>
    /// Builds a CSS URL
    /// </summary>
    /// <param name="cssFile">The name of the CSS file</param>
    public static string CssUrl(string cssFile)
    {
        string result = string.Format("{0}/{1}", CssRoot, cssFile);
        return result;
    }

    /// <summary>
    /// Renders a Web Application MVC specific user control
    /// </summary>
    /// <param name="controlName">Name of the control</param>
    /// <param name="data">ViewData to pass in</param>
    public static string RenderAppControl(this System.Web.Mvc.HtmlHelper helper, string controlName, object data)
    {
        return RenderAppControl(helper, controlName, data, null);
    }

    /// <summary>
    /// Renders a Web Application MVC-specific user control
    /// </summary>
    /// <param name="controlName">Name of the control</param>
    /// <param name="data">ViewData to pass in</param>
    /// <param name="propertySettings">Settings for the control</param>
    public static string RenderAppControl(this System.Web.Mvc.HtmlHelper helper,
    string controlName, object data, object propertySettings)
    {
        string controlPath = string.Format("~/Views/Shared/{0}", controlName);
        string absControlPath = VirtualPathUtility.ToAbsolute(controlPath);

        return helper.RenderUserControl(absControlPath, data, propertySettings);
    }



    /// <summary>
    /// Determines whether the site recognizes the current user
    /// </summary>
    public static bool UserIsLoggedIn()
    {
        bool result = false;
        if (HttpContext.Current != null)
            result = HttpContext.Current.User.Identity.IsAuthenticated;

        return result;
    }

    //public static string GetBlogServiceOR Just run the check if use has acces tot he blog

    /// <summary>
    /// Gets a username for the currently active user
    /// </summary>
    public static string GetUserName()
    {
        string result = Guid.NewGuid().ToString();

        if (HttpContext.Current != null)
        {
            //if not, do we know who they are?
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                result = HttpContext.Current.User.Identity.Name;
                
                //do they have a cookie?
            }
            else if (HttpContext.Current.Request.Cookies["shopper"] != null)
            {
                result = HttpContext.Current.Request.Cookies["shopper"].Value;

            }

            HttpContext.Current.Response.Cookies["shopper"].Value = result;
            HttpContext.Current.Response.Cookies["shopper"].Expires = DateTime.Now.AddMonths(12);
        }


        return result;
    }
}
